package com.yuexun.dhdoctor.module.survey.ui;

import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseEntity;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.baselistener.ISelAndSearchCallBack;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.survey.bean.SurveyTypeBean;
import com.yuexun.dhdoctor.module.survey.model.impl.SurveyModel;
import com.yuexun.dhdoctor.module.survey.presenter.impl.SurveyTypePresenter;
import com.yuexun.dhdoctor.module.survey.ui.fragment.Frag_Survey_DraftBox;
import com.yuexun.dhdoctor.module.survey.ui.fragment.Frag_Survey_Get;
import com.yuexun.dhdoctor.module.survey.ui.fragment.Frag_Survey_Submit;
import com.yuexun.dhdoctor.module.survey.ui.view.ISurveyTypeView;
import com.yuexun.dhdoctor.util.InputMethodUtils;
import com.yuexun.dhdoctor.widget.ActionSheetDialog;

import org.kymjs.kjframe.ui.KJFragment;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by wangt on 2017/8/3.
 */

public class Act_Survey_Main extends BaseKJActivity implements ISurveyTypeView {

    @Bind(R.id.tv_select)
    TextView tvSelect;
    @Bind(R.id.et_search)
    EditText etSearch;

    @Bind(R.id.tv_survey_get)
    TextView tvGet;
    @Bind(R.id.img_survey_get)
    ImageView imgGet;

    @Bind(R.id.tv_survey_submit)
    TextView tvSubmit;
    @Bind(R.id.img_survey_submit)
    ImageView imgSubmit;

    @Bind(R.id.tv_survey_draftbox)
    TextView tvDraftbox;
    @Bind(R.id.img_survey_draftbox)
    ImageView imgDraftbox;

    @Bind(R.id.ll_get)
    LinearLayout llGet;
    @Bind(R.id.ll_submit)
    LinearLayout llSubmit;

    int pageType = 0;
    KJFragment mContentFragment;
    KJFragment fragment_get;
    KJFragment fragment_submit;
    KJFragment fragment_draftbox;
    ISelAndSearchCallBack mCallBack;

    private String searchType = "-1";
    private SurveyTypePresenter presenter;
    private List<SurveyTypeBean> typeDatas = new ArrayList<>();

    public void setOnSelAndSearchCallBack(ISelAndSearchCallBack callBack) {
        this.mCallBack = callBack;
    }

    @Override
    public void setRootView() {
        super.setRootView();
        setContentView(R.layout.act_survey_main);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();

        presenter = new SurveyTypePresenter(new SurveyModel(), this);
        ApiGetData();

        fragment_get = new Frag_Survey_Get();
        fragment_submit = new Frag_Survey_Submit();
        fragment_draftbox = new Frag_Survey_DraftBox();
        mContentFragment = fragment_get;
        setOnSelAndSearchCallBack((ISelAndSearchCallBack) mContentFragment);
        changeFragment(R.id.survey_content, mContentFragment);

        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_SEARCH) {
                    mCallBack.searchBack(searchType, etSearch.getText().toString());
                    InputMethodUtils.closeSoftKeyboard(aty);
                }
                return false;
            }
        });
    }

    //获取分类数据
    private void ApiGetData() {
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuid", PreferenceHelper.readString(this, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        presenter.getSurveyTypeList(params);
    }

    @OnClick({R.id.back, R.id.rl_survey_get, R.id.rl_survey_submit, R.id.rl_survey_draftbox,
            R.id.tv_search, R.id.tv_select})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.rl_survey_get:
                tvGet.setTextColor(getResources().getColor(R.color.app_theme_color));
                imgGet.setBackgroundColor(getResources().getColor(R.color.app_theme_color));
                tvSubmit.setTextColor(getResources().getColor(R.color.txt_black1));
                imgSubmit.setBackgroundColor(getResources().getColor(R.color.transparent));
                tvDraftbox.setTextColor(getResources().getColor(R.color.txt_black1));
                imgDraftbox.setBackgroundColor(getResources().getColor(R.color.transparent));
                mContentFragment = fragment_get;
                setOnSelAndSearchCallBack((ISelAndSearchCallBack) mContentFragment);
                changeFragment(R.id.survey_content, mContentFragment);
                if (pageType != 0) {
                    tvSelect.setText("全部");
                    etSearch.setText("");
                    llGet.setVisibility(View.VISIBLE);
                    llSubmit.setVisibility(View.GONE);
                    pageType = 0;
                }
                break;
            case R.id.rl_survey_submit:
                tvGet.setTextColor(getResources().getColor(R.color.txt_black1));
                imgGet.setBackgroundColor(getResources().getColor(R.color.transparent));
                tvSubmit.setTextColor(getResources().getColor(R.color.app_theme_color));
                imgSubmit.setBackgroundColor(getResources().getColor(R.color.app_theme_color));
                tvDraftbox.setTextColor(getResources().getColor(R.color.txt_black1));
                imgDraftbox.setBackgroundColor(getResources().getColor(R.color.transparent));
                mContentFragment = fragment_submit;
                setOnSelAndSearchCallBack((ISelAndSearchCallBack) mContentFragment);
                changeFragment(R.id.survey_content, mContentFragment);
                if (pageType != 1) {
                    tvSelect.setText("全部");
                    etSearch.setText("");
                    llGet.setVisibility(View.GONE);
                    llSubmit.setVisibility(View.VISIBLE);
                    pageType = 1;
                }
                break;
            case R.id.rl_survey_draftbox:
                tvGet.setTextColor(getResources().getColor(R.color.txt_black1));
                imgGet.setBackgroundColor(getResources().getColor(R.color.transparent));
                tvSubmit.setTextColor(getResources().getColor(R.color.txt_black1));
                imgSubmit.setBackgroundColor(getResources().getColor(R.color.transparent));
                tvDraftbox.setTextColor(getResources().getColor(R.color.app_theme_color));
                imgDraftbox.setBackgroundColor(getResources().getColor(R.color.app_theme_color));
                mContentFragment = fragment_draftbox;
                setOnSelAndSearchCallBack((ISelAndSearchCallBack) mContentFragment);
                changeFragment(R.id.survey_content, mContentFragment);
                if (pageType != 2) {
                    tvSelect.setText("全部");
                    etSearch.setText("");
                    llGet.setVisibility(View.VISIBLE);
                    llSubmit.setVisibility(View.GONE);
                    pageType = 2;
                }
                break;
            case R.id.tv_search:
                mCallBack.searchBack(searchType, etSearch.getText().toString());
                InputMethodUtils.closeSoftKeyboard(aty);
                break;
            case R.id.tv_select:
                selectTypeDialog();
                break;
        }
    }

    @Override
    public void showSurveyTypeList(BaseEntity<SurveyTypeBean> bean) {
        if ("200".equals(bean.getStatus())) {
            typeDatas = bean.getDatas();
        } else {
            ViewInject.toast(bean.getMessage());
        }
    }

    /**
     * 选择类型
     */
    private void selectTypeDialog() {
        ActionSheetDialog actionSheetDialog;
        actionSheetDialog = new ActionSheetDialog(this).builder()
                .setCancelable(true)
                .setCanceledOnTouchOutside(true);

        actionSheetDialog.addSheetItem("全部", ActionSheetDialog.SheetItemColor.Blue,
                new ActionSheetDialog.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int which) {
                        tvSelect.setText("全部");
                        searchType = "-1";
                        mCallBack.searchBack(searchType, etSearch.getText().toString());
                    }
                });
        for (int i = 0; i < typeDatas.size(); i++) {
            final int final1 = i;
            actionSheetDialog.addSheetItem(typeDatas.get(i).getTypeName(), ActionSheetDialog.SheetItemColor.Blue,
                    new ActionSheetDialog.OnSheetItemClickListener() {
                        @Override
                        public void onClick(int which) {
                            tvSelect.setText(typeDatas.get(final1).getTypeName());
                            searchType = typeDatas.get(final1).getTemplateTypeId() + "";
                            mCallBack.searchBack(searchType, etSearch.getText().toString());
                        }
                    });
        }
        actionSheetDialog.show();
    }
}
